package net.starwifi.duobao.service.impl;

import net.starwifi.duobao.entity.DBCode;
import net.starwifi.duobao.entity.DBOrder;
import net.starwifi.duobao.entity.DBTimes;
import net.starwifi.duobao.mvc.Filter;
import net.starwifi.duobao.mvc.Page;
import net.starwifi.duobao.mvc.PageView;
import net.starwifi.duobao.service.DBCodeService;
import net.starwifi.duobao.service.DBOrderService;
import net.starwifi.duobao.service.DBTimesDataService;
import net.starwifi.duobao.service.DBTimesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.*;

/**
 * Created by Administrator on 2015/12/10.
 */
@Service
public class DBTimesDataServiceImpl implements DBTimesDataService {

    @Autowired
    DBTimesService dbTimesService;

    @Autowired
    DBOrderService  dbOrderService;

    @Autowired
    DBCodeService dbCodeService;

    @Autowired
    JedisPool jedisPool;


    public void reBuildDBTimesPool(DBTimes dbTimes){
        if(dbTimes==null){
            return;
        }
        if(dbTimes.getIsLottery()){
            return;
        }
         //生成新的夺宝池号码
        Map data=new HashMap<Integer,Integer>();
        for(int i=0;i<dbTimes.getQuantity();i++){
            data.put(i,i);
        }
        //去除已经分配的夺宝码
        PageView pageView=new PageView(0,20);
        pageView.addFilter(Filter.eq("paymentStatus", 1));
        pageView.addFilter(Filter.eq("timesNo", dbTimes.getTimesNo()));
        pageView.setNumPerPage(20); //每次查询20行
        Long total=dbOrderService.getMQueryCount(pageView.getFilters()); //记录总数
        int totalPage=(int) Math.ceil((double) total / (double) 20); //总页数
        try {
            for(int i=1;i<=totalPage;i++){
                pageView.setPageNumber(i); //当前页
                Page<DBOrder> page = dbOrderService.getScrollData(pageView);
                for (DBOrder dbOrder : page.getContent()) {
                   try {
                       String codeInfo=dbOrder.getCodeInfo();
                       if(codeInfo!=null){
                           if(codeInfo.length()>5){
                               String[] codes=codeInfo.split(",");
                               for(String c:codes){
                                   data.remove(Integer.parseInt(c)-10000000);
                               }
                           }
                       }
                   }catch (Exception e){
                       e.printStackTrace();
                       System.out.println("--------error,orderId:"+dbOrder.getId());
                   }
                }

            }
        }catch (Exception e){
            e.printStackTrace();
        }
        //将Map转变为List
        List<Integer> poolList = new ArrayList<Integer>(data.keySet());
        Collections.shuffle(poolList);
        Jedis jedis=jedisPool.getResource();
        for(int j=0;j<poolList.size();j++){
            System.out.println(dbTimes.getTimesNo()+"  "+poolList.get(j));
            jedis.lpush(dbTimes.getTimesNo(), poolList.get(j) + "");
        }
        jedisPool.returnResource(jedis);
    }




    /** 对未正确发放夺宝码的进行发放，，*/
    @Transactional
    public void reCode(DBTimes dbTimes){
        if(dbTimes==null){
            return;
        }
        if(dbTimes.getIsLottery()){
            return;
        }
        PageView pageView=new PageView(0,20);
        pageView.addFilter(Filter.eq("paymentStatus", 1));
        pageView.addFilter(Filter.eq("timesNo", dbTimes.getTimesNo()));
        pageView.addFilter(Filter.eq("codeInfo", ""));
        pageView.addFilter(Filter.gt("id", 5100L));
        pageView.setNumPerPage(20); //每次查询20行
        Long total=dbOrderService.getMQueryCount(pageView.getFilters()); //记录总数
        int totalPage=(int) Math.ceil((double) total / (double) 20); //总页数
        try {
            for(int i=1;i<=totalPage;i++){
                pageView.setPageNumber(i); //当前页
                Page<DBOrder> page =dbOrderService.getScrollData(pageView);
                for (DBOrder data : page.getContent()) {
                    System.out.println("---------------orderId:"+data.getId());
                    if(data.getId()<5100){
                        continue;
                    }
                    //分配夺宝号
                    String codeInfo="";
                    for(int j=0;j<data.getQuantity();j++){
                        try {
                            DBCode dbCode=new DBCode();
                            dbCode.setTimesNo(data.getTimesNo());
                            dbCode.setDbOrder(data);
                            String code=dbTimesService.getTimesCode(data.getTimesNo());
                            if(code!=null){
                                Integer lastCode=10000000+Integer.parseInt(code);
                                dbCode.setDbCode(lastCode);
                                dbCodeService.save(dbCode);
                                codeInfo=codeInfo+""+lastCode+",";
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                    data.setCodeInfo(codeInfo);
                    dbOrderService.update(data);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
